#!/bin/bash
set -x
DT="tag_new"
DT_CS="tag_new_cs"
REMOTEDIR=/home/work/changsha_project/label_data
DATADIR=/home/dig/airflow/apps/graph-utils/hfile2json/sync-dir
VERSION_FILE="label_version_cs.txt"
CONSISTENCE_FILE="cs_file_md5.txt"
REMOTE_ADDR=172.18.180.228

cd ${DATADIR}

cur_version=''
if [ -f ${VERSION_FILE} ]; then
    cur_version=`cat ${VERSION_FILE}`
fi

new_version=`cat ${DT}.tgz | md5sum | awk '{print $1}'`
file_len=`ls -l ${DT}.tgz | awk '{print $5}'`
#版本大小小于3G，有问题
if [ ${file_len} -lt 3221225472 ]; then
    echo "this is a error msg,please check out"
    exit 0
fi
if [ "$cur_version" == "$new_version" ]; then
    echo "version is the same,exit"
    exit 0
fi

echo "当前MD5: ${cur_version}"
echo "旧版MD5: ${new_version}"

# 先解压
tar -zxf ${DT}.tgz

# 再筛选数据压缩
mkdir -p ${DT_CS}
for file in `ls tag-data/`
do
    touch ${DT_CS}/${file}
    grep '\"province\":\"湖南\"\|\"province\":\"广东\"' tag-data/${file} > ${DT_CS}/${file}
done
tar -zcf ${DT_CS}.tgz ${DT_CS}

# 压缩文件计算md5
cs_md5=`cat ${DT_CS}.tgz | md5sum | awk '{print $1}'`
echo -n ${cs_md5} > ${CONSISTENCE_FILE}


try_cnt=0
while [ ${try_cnt} -lt 4 ];
do
    scp ${DT_CS}.tgz work@${REMOTE_ADDR}:${REMOTEDIR}/
    result=`ssh work@${REMOTE_ADDR} "sh" ${REMOTEDIR}"/cat_cs_md5.sh"`
    if [ "$cs_md5" != "$result" ];then
        echo "sync tag-file failed"
    else
        # 发送MD5文件
        ssh work@${REMOTE_ADDR} "echo ${cs_md5} > ${REMOTEDIR}/${DT_CS}.md5"
        break
    fi
    try_cnt=`expr ${try_cnt} + 1`
    if [ ${try_cnt} -ge 4 ];then
        echo "label 数据传输失败了: ${DT_CS}.tgz"
        exit -1
    fi

done

ssh work@${REMOTE_ADDR} "sh" ${REMOTEDIR}"/upload_label_data_to_changsha.sh"

echo -n ${new_version} > ${VERSION_FILE}
rm -rf ./tag-data
